package com.salton123.soulove.sailfish.video.ui.adapter

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.salton123.app.BaseApplication
import com.salton123.log.XLog
import com.salton123.soulove.api.RouterManager
import com.salton123.soulove.app.R
import com.salton123.soulove.sailfish.db.SailfishDatabase
import com.salton123.soulove.sailfish.video.SailfishManager
import com.salton123.soulove.utils.Utils
import com.salton123.util.ScreenUtils
import com.salton123.widget.HorizontalEvenItemDecoration

/**
 * Time:2021/9/13 4:30 下午
 * Author:
 * Description:
 */
class VideoHomeHorizontalAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.adapter_item_recycler_content) {
    private val decoration = HorizontalEvenItemDecoration(ScreenUtils.dp2px(6f))
    private val differCallback = VideoDifferCallback()
    override fun convert(helper: BaseViewHolder, dirName: String) {
        val recyclerView = helper.getView<RecyclerView>(R.id.recyclerView)
        val horizontalVideoListAdapter = HorizontalVideoListAdapter()
        recyclerView.adapter = horizontalVideoListAdapter
//        horizontalVideoListAdapter.setDiffCallback(differCallback)
        recyclerView.layoutManager = LinearLayoutManager(BaseApplication.sInstance, RecyclerView.HORIZONTAL, false)
        if (recyclerView.itemDecorationCount == 0) {
            recyclerView.addItemDecoration(decoration)
        }

        recyclerView.setHasFixedSize(true)
        (recyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        horizontalVideoListAdapter.setOnItemClickListener { adapter, view, position ->
            SailfishManager.toPlayVideoList = horizontalVideoListAdapter.data
            RouterManager.videoPlay(helper.itemView.context, position)
        }
        helper.getView<TextView>(R.id.tvCount).setOnClickListener {
            SailfishManager.toBatchOperateList = horizontalVideoListAdapter.data
            RouterManager.batchOperate(helper.itemView.context, dirName)
        }
        val param = Utils.sortParam()
        SailfishDatabase.Impl.videoDao().loadDataByDir(dirName, param.first, param.second).observeForever { dataList ->
            XLog.i("VideoHomeHorizontalAdapter", "dirName:$dirName,size:${dataList.size}")
            helper.setText(R.id.tvDirName, dirName).setText(R.id.tvCount, "${dataList.size}")
            horizontalVideoListAdapter.setNewInstance(dataList)
        }
    }
}